﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.CodeDom.Compiler;
using Flame.Dlr;
using Flame.Reflection;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Drawing;
using Flame.Controls.Common;

namespace Flame.Controls
{
    /*public interface IIntellisenseUI
    {
        void TryShow();
    }


    class LanguageOptions
    {
        public Color Color { get; set; }
        public int SmartIndent { get; set; }
    }

    public class TextBoxInput : RichTextBox, ITextInput
    {
        Dictionary<Languages, LanguageOptions> compilersStyleMap = new Dictionary<Languages, LanguageOptions>();

        Languages _language;
        public Languages Language { get { return _language; } set { change(value); } }

        public event EventHandler OnResizeHeight;
        public event EventHandler AskForFocusToNext;
        public event EventHandler AskForFocusToPrevious;
        public event EventHandler OnMoveCaretOnScreen;
        public event EventHandler<MouseEventArgs> MouseWheelFromChilds;
        public event EventHandler<CompilerResultEventArgs> OnCompile;
        const int IntraLineDistance = 0;
        const int LineGap = 5;
        IExecEngines _compilers;

        Dictionary<Languages, IIntellisense> _intellisenseMap = new Dictionary<Languages, IIntellisense>();

        public static Func<ITextInput> ITextInputCreator(Manager compilers) { return new Func<ITextInput>(() => { return new TextBoxInput(compilers); }); }
        public static Func<Manager, ITextInput> ITextInputCreator() { return new Func<Manager, ITextInput>((c) => { return new TextBoxInput(c); }); }

        public TextBoxInput(IExecEngines compilers)
        {
            _language = Languages.Python;

            _compilers = compilers;
            AutoStart = false;

            compilersStyleMap.Add(Languages.Ruby, new LanguageOptions() { Color = Color.LightPink, SmartIndent = 0 });
            compilersStyleMap.Add(Languages.Python, new LanguageOptions() { Color = Color.LightSeaGreen, SmartIndent = 0 });
            compilersStyleMap.Add(Languages.Powershell, new LanguageOptions() { Color = Color.LightGray, SmartIndent = 0 });
            compilersStyleMap.Add(Languages.CSharp, new LanguageOptions() { Color = Color.LightBlue, SmartIndent = 0 });
            compilersStyleMap.Add(Languages.CSharpScript, new LanguageOptions() { Color = Color.LightBlue, SmartIndent = 0 });
            compilersStyleMap.Add(Languages.Javascript, new LanguageOptions() { Color = Color.LightSalmon, SmartIndent = 0 });

            _intellisenseMap.Add(Languages.Ruby, new RubyIntellisense(_compilers.GetEngine(Languages.Ruby)));
            _intellisenseMap.Add(Languages.Python, new PythonIntellisense(_compilers.GetEngine(Languages.Python)));

            base.WordWrap = false;

            setEvents();

            change(Languages.Ruby);
        }

        void setEvents()
        {

            MouseWheel += (sender, e) => { if (MouseWheelFromChilds != null) MouseWheelFromChilds(sender, e); };
            TextChanged += (sender, e) => { if (Lines.Length > 0) { OnResizeHeight(sender, e); } };
            KeyDown += (sender, e) =>
            {
                if (!e.Control && !e.Alt && e.Shift && e.KeyCode == Keys.Enter)
                {
                    Compile();
                    e.SuppressKeyPress = true;
                    AskForFocusToNext(sender, e);
                    OnMoveCaretOnScreen(sender, e);
                    OnResizeHeight(sender, e);
                    return;
                }
                else if (e.Control && !e.Alt && e.Shift && e.KeyCode == Keys.Enter)
                {
                    _compilers.Execute(_language, base.SelectedText);
                    e.SuppressKeyPress = true;
                    return;
                }
                else if (!e.Control && !e.Alt && !e.Shift && (e.KeyCode == Keys.PageUp || e.KeyCode == Keys.PageDown))
                {
                    if (e.KeyCode == Keys.PageUp)
                    {
                        AskForFocusToPrevious(sender, e);
                    }
                    else
                    {
                        AskForFocusToNext(sender, e);
                    }
                }
                else if (e.Control && !e.Alt && !e.Shift && e.KeyCode == Keys.Space)
                {
                    e.Handled = true;
                    e.SuppressKeyPress = true;
                }
            };
        }


        public bool AutoStart { get; set; }

        public void SetParent(Panel panel)
        {
            Dock = DockStyle.Fill;
            panel.Controls.Add(this);
        }

        public Languages LanguageName { get { return _language; } set { change(value); } }

        void change(Languages name)
        {
            _language = name;
        }

        //public ICompiler CurrentCompilerEngine { get { return _compilers.GetCompilerEngine(_language); } }

        public Result Compile(string text) { return _compilers.Execute(_language, text); }
        public IIntellisense CurrentIntellisenseEngine 
        {
            get
            {
                if (!_intellisenseMap.ContainsKey(_language)) return null;
                return _intellisenseMap[_language];
            }
        }

        public int CaretOnScreenPosY
        {
            get
            {
                return PointToScreen(GetPositionFromCharIndex(0)).Y;// PointYFromPosition(Caret.Position))).Y;
            }
        }

        public void Compile()
        {
            OnCompile(this, new CompilerResultEventArgs(_compilers.Execute(_language, Text)));
        }


        public bool IsEmpty
        {
            get { return Text == ""; }
        }


        //TODO serializzare il linguaggio in uso per questo componente
        public MemoryStream ToMemoryStream(int version)
        {
            MemoryStream stream = new MemoryStream();
            new BinaryFormatter().Serialize(stream, Text);
            new BinaryFormatter().Serialize(stream, _language);
            new BinaryFormatter().Serialize(stream, AutoStart);
            return stream;
        }

        public void FromStream(Stream ms, int version)
        {
            Text = (string)new BinaryFormatter().Deserialize(ms);
            if (version <= Versioning.version_150613)
            {
                string s = (string)new BinaryFormatter().Deserialize(ms);
                Languages l;
                if (Enum.TryParse<Languages>(s, true, out l))
                {
                    change(l);
                }
                else
                {
                    change(Languages.None);
                }
            }
            else
            {
                change((Languages)new BinaryFormatter().Deserialize(ms));
            }
            if (version >= 1000)
            {
                AutoStart = (bool)new BinaryFormatter().Deserialize(ms);
            }
        }


     




        public int ContentHeight
        {
            get { return (FontHeight  + IntraLineDistance) * Math.Max(1, Lines.Length) + LineGap; }
        }

      

        public int CurrentPos
        {
            get
            {
                return base.SelectionStart;
            }
            set
            {
                SelectionStart = value;
            }
        }


        public event EventHandler<LanguageChangedEventArgs> OnLanguageChanged;
    }*/
}
